﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using ISRecord.View.Properties;
using ISRecord.View.Services;
using ISRecord.Model;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace ISRecord.View.ViewsModel
{
    public class SettingsViewModel : WorkspaceViewModel
    {
        #region "Fields"
        bool hasChanges = false;
        DelegateCommand saveCommand;
        DelegateCommand resetCommand;
        DelegateCommand specifyLogFileCommand;
        ObservableCollection<Tuple<string, Brush>> backgrounds;
        DelegateCommand backgroundSelectionCommand;
        bool backgroundSelected;
        ReadOnlyCollection<double> fontSizes;
        ReadOnlyCollection<FontFamily> fonts;
        #endregion
        #region "Binding Properties"
        public string ActiveYear
        {
            get { return Settings.Default.ActiveYear; }
            set
            {
                if (Settings.Default.ActiveYear != value)
                {
                    Settings.Default.ActiveYear = value;
                    RaisePropertyChanged("ActiveYear");
                }
            }
        }
        public string LogFilePath
        {
            get { return Settings.Default.LogFilePath; }
            set
            {
                if (Settings.Default.LogFilePath != value)
                {
                    Settings.Default.LogFilePath = value;
                    RaisePropertyChanged("LogFilePath");
                }
            }
        }
        public string TextBeforOutboxSubject
        {
            get { return Settings.Default.PrefixBeforSubject; }
            set
            {
                if (Settings.Default.PrefixBeforSubject != value)
                {
                    Settings.Default.PrefixBeforSubject = value;
                    RaisePropertyChanged("TextBeforOutboxSubject");
                }
            }
        }
        public string TextAfterOutboxSubject
        {
            get { return Settings.Default.PrefixAfterSubject; }
            set
            {
                if (Settings.Default.PrefixAfterSubject != value)
                {
                    Settings.Default.PrefixAfterSubject = value;
                    RaisePropertyChanged("TextAfterOutboxSubject");
                }
            }
        }

        public ObservableCollection<Tuple<string, Brush>> Backgrounds
        {
            get
            {
                if (backgrounds == null)
                {
                    backgrounds = new ObservableCollection<Tuple<string, Brush>>();
                    backgrounds.Add(new Tuple<string, Brush>(
                         AvailableBackgrounds.LightBlue,
                        (Brush)App.Current.FindResource(AvailableBackgrounds.LightBlue)));

                    backgrounds.Add(new Tuple<string,Brush>(
                         AvailableBackgrounds.LightSlateGray, 
                        (Brush)App.Current.FindResource(AvailableBackgrounds.LightSlateGray)));

                    backgrounds.Add(new Tuple<string, Brush>(
                        AvailableBackgrounds.LightGoldenrodYellow,
                        (Brush)App.Current.FindResource(AvailableBackgrounds.LightGoldenrodYellow)));
                    backgrounds.Add(new Tuple<string, Brush>(
                        AvailableBackgrounds.LightGreen,
                        (Brush)App.Current.FindResource(AvailableBackgrounds.LightGreen)));
                      
                }
                return backgrounds;
            }
        }
        public bool BackgroundSelected
        {
            get
            {
                return backgroundSelected;
            }
            set
            {
                if (value != backgroundSelected)
                {
                    backgroundSelected = value;
                    RaisePropertyChanged("BackgroundSelected");
                }
            }
        }
        public string Background
        {
            get
            {
                return Settings.Default.Background; ;
            }
            set
            {
                if (value != Settings.Default.Background)
                {
                    Settings.Default.Background = value;
                    RaisePropertyChanged("Background");
                }
            }
        }
        public ICollection<FontFamily> SystemFonts
        {
            get 
            {
                if(fonts == null)
                {
                    List<FontFamily> list = new List<FontFamily>();
                    list.AddRange(Fonts.SystemFontFamilies);
                    list.Sort((x, y) => x.Source.CompareTo(y.Source));
                    fonts = new ReadOnlyCollection<FontFamily>(list);
                }
                return fonts;
            }
        }
        public ReadOnlyCollection<double> FontSizes
        {
            get {

                if (fontSizes == null)
                {
                    List<double> list = new List<double>();
                    list.Add(8);
                    list.Add(10);
                    list.Add(12);
                    list.Add(14);
                    list.Add(16);
                    list.Add(18);
                    list.Add(20);
                    list.Add(22);
                    list.Add(24);
                    list.Add(26);
                    list.Add(28);
                    list.Add(36);
                    list.Add(48);
                    list.Add(72);
                    fontSizes = new ReadOnlyCollection<double>(list);
                }
                return fontSizes;
            }
        }
        public string SelectedFont
        {
            get { return Settings.Default.Font; }
            set
            {
                if (value != Settings.Default.Font)
                {
                    Settings.Default.Font = value;
                    RaisePropertyChanged("SelectedFont");
                    
                }
            }
        }

        public double FontSize
        {
            get { return Settings.Default.FontSize; }
            set
            {
                if (value != Settings.Default.FontSize)
                {
                    Settings.Default.FontSize = value;
                    RaisePropertyChanged("FontSize");

                }
            }
        }
        #endregion

        #region "Commands"
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new DelegateCommand(Save);
                }
                return saveCommand;
            }
        }
        private void Save(object obj)
        {
            Settings.Default.Save();
            hasChanges = false;
        }

        public ICommand ResetCommand
        {
            get
            {
                if (resetCommand == null)
                {
                    resetCommand = new DelegateCommand(Reset);
                }
                return resetCommand;
            }
           
        }
        public ICommand BackgroundSelectionCommand
        {
            get
            {
                if (backgroundSelectionCommand == null)
                {
                    backgroundSelectionCommand = new DelegateCommand(param => SelectBackground(param));
                }
                return backgroundSelectionCommand;
            }
	
        }
        void SelectBackground(object param)
        { 
            string b = param as string;
            if (!string.IsNullOrEmpty(b))
            {
                Background = b;
            }
        }
        private void Reset(Object obj)
        {
            Settings.Default.Reset();
            hasChanges = false;
        }

        public ICommand SpecifyLogFileCommand
        {
            get
            {
                if (specifyLogFileCommand == null)
                {
                    specifyLogFileCommand = new DelegateCommand(param => SpecifyFilePath());
                }
                return specifyLogFileCommand;
            }
        }

        void SpecifyFilePath()
        {
            var foldersBrowser = new System.Windows.Forms.FolderBrowserDialog();
            if (foldersBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LogFilePath = foldersBrowser.SelectedPath;
            }
        }
        #endregion
        public SettingsViewModel()
        {
            base.DisplayName = Resources.OptionsDispalyName;  
        }

        #region "Base"
        protected override void RaisePropertyChanged(string propertyName)
        {
            hasChanges = true;
            base.RaisePropertyChanged(propertyName);

        }
        public override bool ModelHasChanges
        {
            get
            {
                return hasChanges;
            }
        }
        #endregion

        private struct AvailableBackgrounds
        {
            public const string LightBlue = "LightBlue";
            public const string LightSlateGray = "LightSlateGray";
            public const string LightGoldenrodYellow = "LightGoldenrodYellow";
            public const string LightGreen = "LightGreen";
        }
    }

}
